using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// Summary description for ClientAccountSnapshot
/// </summary>
public class ClientAccountSnapshot
{
    private int accountId;
    private int relativeInvoiceId;
    private decimal invoicesExclusive;
    private decimal invoicesToday;
    private decimal paymentsToday;
    private decimal creditsToday;
    private decimal previousBalance;

    public int AccountId
    {
        get { return accountId; }
        set { accountId = value; }
    }

    public int RelativeInvoiceId
    {
        get { return relativeInvoiceId; }
        set { relativeInvoiceId = value; }
    }

    public decimal InvoicesExclusive
    {
        get { return invoicesExclusive; }
        set { invoicesExclusive = value; }
    }

    public decimal InvoicesToday
    {
        get { return invoicesToday; }
        set { invoicesToday = value; }
    }

    public decimal PaymentsToday
    {
        get { return paymentsToday; }
        set { paymentsToday = value; }
    }

    public decimal CreditsToday
    {
        get { return creditsToday; }
        set { creditsToday = value; }
    }

    public decimal PreviousBalance
    {
        get { return previousBalance; }
        set { previousBalance = value; }
    }

    /// <summary>
    /// Parameterless constructor for serialization.
    /// </summary>
    public ClientAccountSnapshot() { }

    public ClientAccountSnapshot(
        int accountId,
        int relativeInvoiceId,
        decimal invoicesExclusive,
        decimal invoicesToday,
        decimal paymentsToday,
        decimal creditsToday,
        decimal previousBalance)
    {
        this.accountId = accountId;
        this.relativeInvoiceId = relativeInvoiceId;
        this.invoicesExclusive = invoicesExclusive;
        this.invoicesToday = invoicesToday;
        this.paymentsToday = paymentsToday;
        this.creditsToday = creditsToday;
        this.previousBalance = previousBalance;
    }

    public static ClientAccountSnapshot Fetch(int accountId, int relativeInvoiceId, DateTime snapshotDate)
    {
        ClientAccountSnapshot snapshot;
        using (SqlConnection connection = new SqlConnection(Settings.ConnectionStrings.AccountsDatabase))
        {
            SqlCommand command = new SqlCommand("ClientAccountSnapshotFetch", connection);
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.AddWithValue("accountId", accountId);
            command.Parameters.AddWithValue("relativeInvoiceId", relativeInvoiceId);
            command.Parameters.AddWithValue("snapshotDate", snapshotDate.Date);

            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            if (!reader.Read())
            {
                // Account must be empty, as nothing was found.
                snapshot = new ClientAccountSnapshot(accountId, relativeInvoiceId, 0, 0, 0, 0, 0);
            }
            else
            {
                snapshot = new ClientAccountSnapshot(
                    accountId, relativeInvoiceId,
                    (decimal)reader["InvoicesExclusive"],
                    (decimal)reader["InvoicesToday"],
                    (decimal)reader["PaymentsToday"],
                    (decimal)reader["CreditsToday"],
                    (decimal)reader["PreviousBalance"]);
            }

            connection.Close();
        }
        return snapshot;
    }
}
